package com.zero.clinic.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zero.clinic.pojo.Dictionary;
import com.zero.clinic.pojo.Exam;
import com.zero.clinic.pojo.ExamExample;
import com.zero.clinic.pojo.MessageObject;
import com.zero.clinic.service.DictionaryService;
import com.zero.clinic.service.ExamService;
import com.zero.clinic.utils.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/exam")
public class ExamController {
    @Resource
    private ExamService examService;

    @Resource
    private DictionaryService dictionaryService;

    /**
     * @function 进入测试项目管理模块
     * @return
     */
    @RequiresPermissions("exam:examPage")
    @RequestMapping("/examPage")
    public String examPage(){
        return "examPage";
    }

    /**
     * @function 测试项目列表
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @ResponseBody
    @RequiresPermissions("exam:list")
    @RequestMapping("/list")
    public PageInfo<Exam> examList(@RequestParam(defaultValue = "1") int pageNum,
                                   @RequestParam(defaultValue = "10") int pageSize, String keywords){
        PageHelper.startPage(pageNum, pageSize);
        // 1.查出所有的Exam信息
        ExamExample example = new ExamExample();
        // 2 根据搜索关键词查询
        if (StringUtils.isNotBlank(keywords)){
            // 2.1 根据测试项目名称查询
            ExamExample.Criteria criteria1= example.createCriteria();
            criteria1.andExamNameLike("%"+ keywords +"%");
            // 2.2 根据测试项目类型名称查询
            ExamExample.Criteria criteria2 = example.or();
            criteria2.andExamTypeNameLike("%"+ keywords +"%");
        }
        List<Exam> exams = examService.selectByExample(example);
        // 3.以PageInfo对象返回给前端
        PageInfo<Exam> pageInfo = new PageInfo<>(exams);
        return pageInfo;
    }

    /**
     * @function 编辑测试项目页面
     * @param examId
     * @param m
     * @return
     */
    @RequestMapping("/edit")
    public String examEdit(Long examId, Model m){

        // 1.如果前端页面需要更新测试项目信息，则会传一个需要更新的examId过来。这里需要通过examId查出更新信息后共享给前端页面
        if (examId != null) {
            Exam exam = examService.selectByPrimaryKey(examId);
            m.addAttribute("exam",exam);
        }

        // 2.将测试项目类型的基础数据查出来共享给前端页面
        List<Dictionary> dictionaries = dictionaryService.selectDictionariesByParentName(Constant.DICTIONARY_EXAM_TYPE);
        m.addAttribute("dictionaries", dictionaries);

        return "examEdit";
    }

    /**
     * @function 新增测试项目
     * @param exam
     * @return
     */
    @ResponseBody
    @RequestMapping("/insert")
    @RequiresPermissions("exam:insert")
    public MessageObject examInsert(Exam exam){
        //1.插入数据
        System.out.println("需要新增的测试项目：" + exam);
        int row = examService.insert(exam);
        if (row > 0) {
            return MessageObject.createMo(1,"新增数据成功");
        }
        return MessageObject.createMo(0,"新增数据失败，请联系技术人员");
    }

    /**
     * @function 更新测试项目
     * @param exam
     * @return
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("exam:update")
    public MessageObject examUpdate(Exam exam){
        //1.更新数据
        System.out.println("需要更新的测试项目" + exam);
        int row = examService.updateByPrimaryKeySelective(exam);
        if (row > 0) {
            return MessageObject.createMo(1,"更新数据成功");
        }
        return MessageObject.createMo(0,"更新数据失败，请联系技术人员");
    }

    /**
     * @function 检查测试项目名是否重复
     * @param examName
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkExamName")
    @RequiresPermissions("exam:checkExamName")
    public boolean examCheckExamName(String examName){
        ExamExample example = new ExamExample();
        ExamExample.Criteria criteria = example.createCriteria();
        criteria.andExamNameEqualTo(examName);
        List<Exam> exams = examService.selectByExample(example);
        return exams.size() > 0 ? false : true;
    }

    /**
     * @function 删除测试项目
     * @param examId
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    @RequiresPermissions("exam:delete")
    public MessageObject examDelete(Long examId){
        //1.根据examId删除数据
        int row = examService.deleteByPrimaryKey(examId);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败，请联系技术人员");
    }

    /**
     * @function 批量删除测试项目
     * @param examIds
     * @return
     */
    @ResponseBody
    @RequestMapping("/batchdel")
    @RequiresPermissions("exam:batchdel")
    public MessageObject examBatchDelete(String examIds){
        String[] examIdsArr = examIds.split(",");
        //1.根据examIdsArr删除数据
        int row = examService.deleteByIds(examIdsArr);
        if (row > 0){
            return MessageObject.createMo(1,"删除数据成功");
        }
        return MessageObject.createMo(0,"删除数据失败");
    }

}
